package com.lazy.app.ui.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.view.ViewCompat;

import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.RoundedCornerTreatment;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.ShapePath;
import com.google.android.material.shape.TriangleEdgeTreatment;
import com.lazy.app.R;
import com.lazy.app.databinding.ActvityLoginBinding;
import com.lazy.app.presenter.H5Presenter;
import com.lazy.app.presenter.LoginPresenter;
import com.zndroid.base.ui.common.CommonActivity;
import com.zndroid.utils.impl.SpannableStringUtil;

import java.util.Objects;

public class LoginActivity extends CommonActivity<ActvityLoginBinding, LoginPresenter> {
    @Override
    protected ActvityLoginBinding onSupplyViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActvityLoginBinding.inflate(layoutInflater);
    }

    @Override
    protected int onSupplyStatusBarColor() {
        return ResourcesCompat.getColor(getResources(), R.color.color_white, getTheme());
    }

    @Override
    protected LoginPresenter onSupplyPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void doOnInitViewAfterAllReady(View rootView) {
        Objects.requireNonNull(currentViewBinding().input.inputPsw.getEditText()).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                currentViewBinding().input.btLogin.setEnabled(!TextUtils.isEmpty(charSequence));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        // description
        currentViewBinding().input.cbDescription.setOnCheckedChangeListener((compoundButton, b) -> {
            if (compoundButton.isPressed()) {
                currentViewBinding().input.btTip.setVisibility(View.GONE);
            }
        });

        SpannableStringUtil.with(currentViewBinding().input.cbDescription)
                .append("勾选代表你同意")
                .append("《用户协议》")
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(currentContext(), H5Activity.class);

                        intent.putExtra(H5Presenter.H5_KEY_TITLE, "用户协议");
                        intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_USER_AGREEMENT);
                        intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_USER_AGREEMENT);
                        intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

                        startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.argb(255, 87, 107, 149));
                        ds.setUnderlineText(true);
                    }
                })
                .append("和")
                .append("《隐私政策》")
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        Intent intent = new Intent(currentContext(), H5Activity.class);

                        intent.putExtra(H5Presenter.H5_KEY_TITLE, "隐私保护政策");
                        intent.putExtra(H5Presenter.H5_KEY_URL, H5Presenter.H5_URL_OF_PRIVACY_POLICY);
                        intent.putExtra(H5Presenter.H5_KEY_TYPE, H5Presenter.H5_TYPE_OF_PRIVACY_POLICY);
                        intent.putExtra(H5Presenter.H5_KEY_USE_NATIVE_TITLE, true);

                        startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        ds.setColor(Color.argb(255, 87, 107, 149));
                        ds.setUnderlineText(true);
                    }
                })
                .append("相关条款")
                .create();

        currentViewBinding().input.btLogin.setOnClickListener(v -> {
            //在华为手机上EditText如果设置 inputType="textPassword"并且焦点在该EditText上，弹出的华为安全键盘会挡住Toast提示
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }

            if (!currentViewBinding().input.cbDescription.isChecked()) {
                ShapeAppearanceModel model = ShapeAppearanceModel
                        .builder()
                        .setAllCorners(new RoundedCornerTreatment())
                        .setAllCornerSizes(20f)
                        .setTopEdge(new TriangleEdgeTreatment(20f, false){
                            @Override
                            public void getEdgePath(float length, float center, float interpolation, @NonNull ShapePath shapePath) {
                                super.getEdgePath(length, center/5, interpolation, shapePath);
                            }
                        })
                        .build();
                MaterialShapeDrawable drawable = new MaterialShapeDrawable(model);
                drawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
                drawable.initializeElevationOverlay(currentActivity());
                drawable.setPaintStyle(Paint.Style.STROKE);

                ViewCompat.setBackground(currentViewBinding().input.btTip, drawable);
                currentViewBinding().input.btTip.setVisibility(View.VISIBLE);

            } else {
                showToast("开发中");
            }
        });

        currentViewBinding().bottom.tvRegister.setOnClickListener(v -> showToast("注册"));
        currentViewBinding().bottom.tvForgot.setOnClickListener(v -> showToast("忘记密码"));
        currentViewBinding().bottom.tvMore.setOnClickListener(v -> showToast("更多"));
    }
}
